package com.example.springbootdemo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//@RequestMapping("/response")
//@RestController

@Controller
public class ResponseController {


//    @RestController
//        @RequestMapping("/index")
//        public String index() {
//            // 返回index.html
//            return "/index.html";
//        }
//    @ResponseBody
//    @RequestMapping("/returnData")
//    public String returnData(){
//        return "/index.html";
//    }


    // 返回视图
    @RequestMapping("/index")
    public Object index() {
        return "/index.html";
    }

    // 返回数据
    @RequestMapping("/returnData")
//    @ResponseBody
    public String returnData() {
        return "该方法返回数据";
    }

    //
//
    @RequestMapping("/returnHtml")
    @ResponseBody
    public String returnHtml() {
        return "<h1>Hello,HTML~</h1>";
    }

    @RequestMapping("/returnHtmlWithEntity")
    public ResponseEntity<String> returnHtmlWithEntity() {
        String htmlContent = "<h1>Hello,HTML~</h1>";

        // 设置Content-Type为text/html，确保浏览器正确解析
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_HTML);

        return new ResponseEntity<>(htmlContent, headers, HttpStatus.OK);
    }


    @RequestMapping("/returnJsonWithEntity")
    public ResponseEntity<List<Person>> returnJsonWithEntity() {
        List<Person> people = new ArrayList<>();
        Person person = new Person("zhangsan", 1, 18);
        people.add(person);

        // 明确设置Content-Type为application/json
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        return new ResponseEntity<>(people, headers, HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/index2")
    public Object index2() {
        return "/a.js"; // 自动设置为application/javascript
    }

    @ResponseBody
    @RequestMapping("/index3")
    public Object index3() {
        return "/b.css"; // 自动设置为text/css
    }
//    @ResponseBody
//    @RequestMapping("/returnJson")
//    public List<Person> returnJson() {
//        List<Person> people = new ArrayList<Person>();
//        Person person = new Person("zhangsan",1,18);
//        people.add(person);
//        return people; // 自动设置为application/javascript
//    }
//
//    @ResponseBody
//    @RequestMapping("/returncss")
//    public Object index3() {
//        return "/bbbbbb.css"; // 自动设置为text/css
//    }

    //    @ResponseBody
//    @RequestMapping("/returnJson")
//    public List<Person> returnJson(){
//        List<Person> people = new ArrayList<>();
//        Person person = new Person("zhangsan", "1", 4);
//        people.add(person);
//        return people;
//    }
//
//    @RequestMapping("/returnJson")
//    @ResponseBody
//    public HashMap<String, String> returnJson() {
//        HashMap<String, String> map = new HashMap<>();
//        map.put("Java", "Java Value");
//        map.put("MySQL", "MySQL Value");
//        map.put("Redis", "Redis Value");
//        return map;
//    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(403);
        return "设置状态码成功";
    }


    @RequestMapping("/setStatus2")
    public ResponseEntity<String> setStatus2() {
        // 使用ResponseEntity设置状态码和响应体
        return ResponseEntity
                .status(HttpStatus.FORBIDDEN) // 设置403状态码
                .body("设置状态码成功"); // 设置响应体内容
    }

    // 方式三：使用ResponseEntity构造函数
    @RequestMapping("/setStatus3")
    public ResponseEntity<String> setStatus3() {
        // 使用ResponseEntity构造函数
        return new ResponseEntity<>("设置状态码成功", HttpStatus.FORBIDDEN);
    }

    @RequestMapping("/setStatus4")
    public ResponseEntity<String> setStatus4() {
        // 使用静态工厂方法创建ResponseEntity
        return ResponseEntity
                .ok() // 设置200状态码
                .header("Custom-Header", "value") // 添加自定义头部
                .body("操作成功"); // 设置响应体
    }

    // 方式四：使用ResponseEntity的静态方法
    @RequestMapping("/setStatus5")
    public ResponseEntity<String> setStatus5() {
        // 使用ResponseEntity的status方法设置状态码
        return ResponseEntity
                .status(HttpStatus.FORBIDDEN) // 设置403状态码
                .header("Custom-Header", "value") // 添加自定义头部
                .body("操作成功"); // 设置响应体
    }
//

    @RequestMapping("/setContentTypeWithEntity")
    public ResponseEntity<String> setContentTypeWithEntity() {
        String jsonContent = "{\"success\":true}";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Charset", "utf-8");

        return new ResponseEntity<>(jsonContent, headers, HttpStatus.OK);
    }
    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("myHeader", "myHeader");
        return "设置Header成功";
    }

    @RequestMapping(value = "/setHeader2")
    public ResponseEntity<String> setHeader2() {
        return ResponseEntity.ok()
                .header("MyHeader", "MyHeaderValue")
                .header("Another-Header", "AnotherValue")
                .body("设置Header成功");
    }


    @RequestMapping(value = "/setHeader3")
    public ResponseEntity<String> setHeader3() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("MyHeader", "MyHeaderValue");
        headers.add("Another-Header", "AnotherValue");
        headers.setContentType(MediaType.APPLICATION_JSON);

        return new ResponseEntity<>("设置Header成功", headers, HttpStatus.OK);
    }

    @RequestMapping(value = "/setContentType", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String returnJson2() {
        return "{\"success\":true}";
    }
}
